<template>
  <transition name="bounce">
    <div class="modal-bg" v-show="show" @mousemove="modalMove" @mouseup="cancelMove">
      <div class="modal-container">
        <div class="modal-header" @mousedown="setStartingPoint">
          {{title}}
        </div>
        <div class="modal-main">
          <slot></slot>
        </div>
        <div class="modal-footer">
          <button round @click="cancel">取消</button>
          <button type="primary" round @click="ok">确认</button>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
  export default {
    name: 'modal',
    props: {
      show: {
        type: Boolean,
        default: false
      },
      title: {
        type: String,
        default: ''
      },
    },
    data() {
      return {
        x: 0,
        y: 0,
        node: null,
        isCanMove: false
      }
    },
    mounted() {
      this.node = document.querySelector('.modal-container')
    },
    methods: {
      cancel() {
        this.$emit('cancel')
      },

      ok() {
        this.$emit('ok')
      },

      setStartingPoint(e) {
        this.x = e.clientX - this.node.offsetLeft
        this.y = e.clientY - this.node.offsetTop
        this.isCanMove = true
      },

      modalMove(e) {
        if (this.isCanMove) {
          this.node.style.left = e.clientX - this.x + 'px'
          this.node.style.top = e.clientY - this.y + 'px'
        }
      },

      cancelMove() {
        this.isCanMove = false
      }
    }
  }
</script>
<style scoped>
  .modal-bg {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0);
    z-index: 10;
  }

  .modal-container {
    background: #fff;
    border-radius: 5px;
    width: 40%;
    height: 40%;
    overflow: hidden;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 255, 255, 0.9);
    box-shadow: 7px 7px 5px 0px rgba(50, 50, 50, 0.75);
  }

  .modal-header {
    height: 40px;
    background-image: linear-gradient(125deg, #2c3e50, #27ae60, #2980b9, #e74c3c, #8e44ad);
    background-size: 400%;
    animation: bganimation 15s infinite;
    /* filter: blur(2px); */

    color: #fff;
    text-align: left;
    padding: 0 5%;
    line-height: 38px;
    cursor: move;
  }

  @keyframes bganimation {
    0% {
      background-position: 0% 50%;
    }

    50% {
      background-position: 100% 50%;
    }

    100% {
      background-position: 0% 50%;
    }
  }


  .modal-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: 40px;
  }

  .modal-footer button {
    width: 100px;
    height: 32px;
    line-height: 28px;
    margin: 0 10px;
    cursor: pointer;
  }

  .modal-main {
    padding: 15px 40px;
    height: calc(100% - 120px);
  }


  /* 过度动画 */
  .bounce-enter-active {
    animation: bounce-in .15s;
  }

  .bounce-leave-active {
    animation: bounce-in .15s reverse;
  }

  @keyframes bounce-in {
    0% {
      transform: scale(0);
    }

    100% {
      transform: scale(1);
    }
  }
</style>
